<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet"href="/css/plat-style.css">
</head>
<body>

<div style="margin-top: 10px;">
    <a href="/bankloan/list" class="layui-btn" style="margin-left: 10px">取消</a>
    <button type="button" class="layui-btn layui-btn-normal" style="margin-left: 10px" id="saveBtn">保存</button>
</div>

<%--隐藏域--%>
<input type="hidden" value="${bankLoan.id}" id="hid">
<input type="hidden" value="${bankLoan.bankLoanId}" id="hbankLoanId">

<div class="layui-tab" style="background-color: white">
    <ul class="layui-tab-title">
        <li class="layui-this">基本信息</li>
        <li>属性</li>
        <li>详情</li>
    </ul>
    <div class="layui-tab-content">
        <%-- 基本信息--%>
        <div class="layui-tab-item layui-show">
            <div class="layui-form-item" style="width:50%">
                <label class="layui-form-label">贷款名称:</label>
                <div class="layui-input-block">
                    <input type="text" name="loanName" id="loanName"  value="${bankLoan.loanName}" lay-verify="title" autocomplete="off" placeholder="请输入贷款名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" style="width: 50%">
                <label class="layui-form-label">是否启用:</label>
                <div class="layui-input-block">
                    <select class="layui-input" id="status">
                        <option value="1">启用</option>
                        <option value="2">禁用</option>
                    </select>
                </div>
            </div>
        </div>
        <%-- 属性--%>
            <div class="layui-tab-item" style="margin-top: 20px">
                <div class="layui-col-sm9" style=" line-height: 25px; font-size: large;">
                    如果添加的属性为：<i style="color: red">单选、复选、下拉框，属性值格式为：xx,xx(英文逗号)，其他属性值可为空。</i>
                </div>
                <table class="layui-table" id="table" lay-filter="table">
                    <thead>
                    <tr>
                        <td>属性类型</td>
                        <td>属性名</td>
                        <td>属性值</td>
                        <td>排序</td>
                        <td>操作</td>
                    </tr>
                    </thead>
                    <tbody>
                         <c:forEach items="${bankLoanSub}" var="list">
                             <tr>
                                 <td>
                                     <input type="hidden" name="subId" value='<c:if test="${list.id!=null}">${list.id}</c:if>'>
                                     <select class="layui-input" name="inputType">
                                         <option value="1" <c:if test="${list.inputType ==1}">selected</c:if>>单选按钮</option>
                                         <option value="2" <c:if test="${list.inputType ==2}">selected</c:if>>复选框</option>
                                         <option value="3" <c:if test="${list.inputType ==3}">selected</c:if>>下拉框</option>
                                         <option value="4" <c:if test="${list.inputType ==4}">selected</c:if>>输入框</option>
                                         <option value="5" <c:if test="${list.inputType ==5}">selected</c:if>>日期选项</option>
                                         <option value="6" <c:if test="${list.inputType ==6}">selected</c:if>>手机号</option>
                                         <option value="7" <c:if test="${list.inputType ==7}">selected</c:if>>省市县区</option>
                                         <option value="8" <c:if test="${list.inputType ==8}">selected</c:if>>身份证</option>
                                         <option value="9" <c:if test="${list.inputType ==9}">selected</c:if>>邮箱</option>
                                     </select>
                                 </td>
                                 <td><input type="text" class="layui-input" value='<c:if test="${list.fieldName!=null}">${list.fieldName}</c:if>' name="fieldName"></td>
                                 <td><input type="text" class="layui-input" value='<c:if test="${list.inputValue!=null}">${list.inputValue}</c:if>' name="inputValue"></td>
                                 <td><input type="text" class="layui-input" value='<c:if test="${list.sort!=null}">${list.sort}</c:if>' name="sort" ></td>
                                 <td><a class="layui-btn layui-btn-danger layui-btn-xs del">删除</a></td>
                             </tr>
                         </c:forEach>
                    </tbody>
                </table>
                <div style="text-align: center"> <a class="layui-btn layui-btn-xs add" style="width: 100px;height: 30px;margin-top: 20px">添加</a></div>
            </div>
        <%--详情--%>
        <div  class="layui-tab-item">
            <div class="layui-col-sm12">
                                    <textarea id="content" name="content"
                                              style="display: none">${bankLoan.loanDetails}</textarea>
                <script id="articleEditor" type="text/plain"
                        style="width:100%;height:400px;"></script>
            </div>
        </div>
    </div>
</div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script type="text/javascript" src="/js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script type="text/javascript" src="/js/jquery.form.js"></script>
<script type="text/javascript" src="/js/plugins/image/previewImg.js"></script>
<script type="text/javascript" src="/js/plugins/dropzone/dropzone.js"></script>
<script type="text/javascript" src="/js/plugins/layui-formSelect/formSelects-v4.min.js"></script>
<script type="text/javascript" src="/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="/js/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript" src="/js/ueditor/lang/zh-cn/zh-cn.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form','laydate'], function(){
        var $ = layui.$,
            form = layui.form,
            element = layui.element,
            layer = layui.layer;
        form.render('select');
        element.render();

        //日期框
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#dateInput' //指定元素
        });
    });


    $(function () {
        /* 富文本编辑器加载 */
        var ue = UE.getEditor('articleEditor');
        UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
        UE.Editor.prototype.getActionUrl = function (action) {
            if (action == '/uploadFile') {
                return '/base/uploadRichFile';
            } else {
                return this._bkGetActionUrl.call(this, action);
            }
        };
        UE.getEditor('articleEditor').ready(function () {
            UE.getEditor('articleEditor').setContent($("#content").text());
        });
        // layui多选插件加载
        var formSelects = layui.formSelects;
        formSelects.value('label');
    });

    // 动态添加属性
    var sortNum=0;
    var inputTypeHtml=`<td>`+
        `<input type="hidden" name="subId">`+
        `<select class="layui-input" name="inputType">`+
        `<option value="1">单选按钮</option>`+
        `<option value="2">复选框</option>`+
        `<option value="3">下拉框</option>`+
        `<option value="4">输入框</option>`+
        `<option value="5">日期选项</option>`+
        `<option value="6">手机号</option>`+
        `<option value="7">省市县区</option>`+
        `<option value="8">身份证</option>`+
        `<option value="9">邮箱</option>`+
        `</select>`+
        `</td>`
    //因为动态添加的元素class属性是无效的，所以不能用$('.add').click(function(){});
    $('body').on('click', '.add', function() {
        var fieldMe=document.getElementsByName("fieldName");
        var inputType=document.getElementsByName("inputType");
        var inputValue=document.getElementsByName("inputValue");
        for (var i = 0; i <fieldMe.length ; i++) {
            if( fieldMe[i].value==''){
                layer.msg('属性名不能为空', {
                    time : 2000
                });
                return;
            }
            var sel=inputType[i];
            var index = sel.selectedIndex; // 选中索引
            var value = sel.options[index].value; // 选中值
            if(value!=4){
                var str=inputValue[i].value;
                // if(!/^\w+(\,\w+)*$/.test(str)){
                //     inputValue[i].style.border="1px solid red";
                //     layer.msg('属性值格式不正确。', {
                //         time : 2000
                //     });
                //     return;
                // }else {
                //     inputValue[i].style.border="1px solid #d9edf7";
                // }
            }
        }
        sortNum=sortNum+1;
        //你要添加的html
        var html = '<tr>'+
            inputTypeHtml+
            '<td><input type="text" class="layui-input" name="fieldName"></td>'+
            '<td><input type="text" class="layui-input" name="inputValue"></td>'+
            '<td><input type="text" class="layui-input" value='+sortNum+' name="sort" ></td>'+
            '<td>'+
            '<a class="layui-btn layui-btn-danger layui-btn-xs del">删除</a>'+
            '</td>'+
            '</tr>';
        //添加到表格最后
        $(html).appendTo($('#table tbody:last'));
    });
    $('body').on('click', '.del', function() {
        if ($('#table tbody tr').length === 1) {
            layer.msg('只有一条不允许删除。', {
                time : 2000
            });
        } else {
            //删除当前按钮所在的tr
            $(this).closest('tr').remove();
            sortNum=sortNum-1;
        }
    });

    var fiedArr={
        bankLoan:{
            "loanName":"",
            "status":"",
            "loanDetails":"",
            "bankLoanId":"",
            "id":""
        },
        "bankLoanFieldMaintainSubs":[],
    };
    $("#saveBtn").click(function(){
        //基本信息
        var loanName=$('#loanName').val();
        if(loanName==''){
            layer.msg('贷款名称不能为空。', {
                time : 2000
            });
            return;
        }

        fiedArr.bankLoan.loanName=loanName;
        //属性信息
        fiedArr.bankLoanFieldMaintainSubs=[];
        //详情
        var loanDetails=UE.getEditor('articleEditor').getContent();
        fiedArr.bankLoan.loanDetails=loanDetails;

        //id
        var id=$('#hid').val();
        fiedArr.bankLoan.id=id;

        //bankLoanId
        var bankLoanId=$('#hbankLoanId').val();
        fiedArr.bankLoan.bankLoanId=bankLoanId;

        var fiedName=document.getElementsByName("fieldName") ;
        var inputType=document.getElementsByName("inputType");
        var inputValue=document.getElementsByName("inputValue");
        var sort=document.getElementsByName("sort");
        var subId=document.getElementsByName("subId");
        for (var i = 0; i <fiedName.length ; i++) {
            if(fiedName[i].value==''){
                fiedName[i].style.border="1px solid red";
                layer.msg('属性名不能为空！。', {
                    time : 2000
                });
                return;
            }
            var sel=inputType[i];
            var index = sel.selectedIndex; // 选中索引
            var value = sel.options[index].value; // 选中值
            // if(value!=4){
            //     if(!/^\w+(\,\w+)*$/.test(inputValue[i].value)){
            //         inputValue[i].style.border="1px solid red";
            //         layer.msg('属性值格式不正确。', {
            //             time : 2000
            //         });
            //         return;
            //     }
            // }
            fiedArr.bankLoanFieldMaintainSubs.push({"id":subId[i].value,"fieldName":fiedName[i].value,"sort":sort[i].value,"inputType":inputType[i].value,"inputValue":inputValue[i].value});
        }
        $.ajax({
            url: "/bankloan/updateDate",
            data: JSON.stringify(fiedArr),
            dataType:'application/json',
            headers: {
                "Content-Type": "application/json",
            },
            type: "post",
            success: function (data) {
                window.location.href="/bankloan/listDate"
            },error:function (data) {

            }
        })
        window.location.href="/bankloan/list"
    });



</script>
</body>
</html>